home *** CD-ROM | disk | FTP | other *** search
/ Software Explosion / Software Explosion (Fore-Matt Home Computing)(1996).iso / games / workbench / lander_2 / source / gameport.c < prev    next >
C/C++ Source or Header  |  1996-01-01  |  2KB  |  106 lines

  1. #include <exec/types.h>
  2. #include <exec/devices.h>
  3. #include <graphics/gfx.h>
  4. #include <devices/gameport.h>
  5. #include <devices/inputevent.h>
  6. #include <intuition/intuition.h>
  7.  
  8. extern int debug;
  9.  
  10. struct InputEvent gameEvent;
  11. struct MsgPort *gameMsgPort;
  12. struct IOStdReq *gameIOMsg;
  13. int ControllerType;
  14. int GameportOpen;
  15. int gameIOPending;
  16.  
  17. trash_joy()
  18. {
  19.     if(gameIOPending &&
  20.         !CheckIO(gameIOMsg)) AbortIO(gameIOMsg);
  21.     if(ControllerType != GPCT_NOCONTROLLER)
  22.         SetControllerType(GPCT_NOCONTROLLER);
  23.     if(GameportOpen) CloseDevice(gameIOMsg);
  24.     if(gameIOMsg) DeleteStdIO(gameIOMsg);
  25.     if(gameMsgPort) DeletePort(gameMsgPort);
  26. }
  27.  
  28. init_joy()
  29. {
  30.     gameIOMsg = 0;
  31.     gameMsgPort = 0;
  32.     ControllerType = GPCT_NOCONTROLLER;
  33.     GameportOpen = 0;
  34.     gameIOPending = 0;
  35.     add_cleanup(trash_joy, "joystick");
  36.     if(!(gameMsgPort = CreatePort(0, 0))) {
  37.         printf("Can't create joystick port\n");
  38.         cleanup(debug);
  39.         exit(20);
  40.     }
  41.     if(!(gameIOMsg = CreateStdIO(gameMsgPort))) {
  42.         printf("Can't create joystick message.\n");
  43.         cleanup(debug);
  44.         exit(20);
  45.     }
  46.     if(OpenDevice("gameport.device", 1, gameIOMsg, 0) != 0) {
  47.         printf("Can't open gameport.device.\n");
  48.         cleanup(debug);
  49.         exit(20);
  50.     }
  51.     GameportOpen = 1;
  52.     if(SetControllerType(GPCT_ABSJOYSTICK) != 0) {
  53.         printf("Can't set controller type.\n");
  54.         cleanup(debug);
  55.         exit(20);
  56.     }
  57.     ControllerType = GPCT_ABSJOYSTICK;
  58.     if(SetControllerTrigger() != 0) {
  59.         printf("Can't set controller trigger.\n");
  60.         cleanup(debug);
  61.         exit(20);
  62.     }
  63.     SendGameIO();
  64. }
  65.  
  66. SendGameIO()
  67. {
  68.     gameIOMsg->io_Command = GPD_READEVENT;
  69.     gameIOMsg->io_Data = (APTR)&gameEvent;
  70.     gameIOMsg->io_Length = sizeof(gameEvent);
  71.     gameIOMsg->io_Flags = 0;
  72.  
  73.     SendIO(gameIOMsg);
  74.     gameIOPending = 1;
  75. }
  76.  
  77. SetControllerType(type)
  78. SHORT type;
  79. {
  80.     BYTE buffer[1];
  81.     gameIOMsg->io_Command = GPD_SETCTYPE;
  82.     gameIOMsg->io_Length = 1;
  83.     gameIOMsg->io_Data = (APTR)buffer;
  84.     buffer[0] = type;
  85.     SendIO(gameIOMsg);
  86.     WaitPort(gameMsgPort);
  87.     GetMsg(gameMsgPort);
  88.     return (int)gameIOMsg->io_Error;
  89. }
  90.  
  91. SetControllerTrigger()
  92. {
  93.     struct GamePortTrigger gpt;
  94.     gameIOMsg->io_Command = GPD_SETTRIGGER;
  95.     gameIOMsg->io_Length = sizeof(gpt);
  96.     gameIOMsg->io_Data = (APTR)&gpt;
  97.     gpt.gpt_Keys = GPTF_UPKEYS|GPTF_DOWNKEYS;
  98.     gpt.gpt_Timeout = 0;
  99.     gpt.gpt_XDelta = 1;
  100.     gpt.gpt_YDelta = 1;
  101.  
  102.     return DoIO(gameIOMsg);
  103. }
  104.  
  105.  
  106.